user  nginx;
worker_processes  4;

error_log  {{ salt['pillar.get']('nginx:error_log', '/var/log/nginx/error.log') }} warn;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 100000;

events {
  worker_connections  1024;
  use epoll;
  multi_accept on;
}


http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  log_format   main   '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  {{ salt['pillar.get']('nginx:access_log', '/var/log/nginx/access.log') }}  main buffer=16k;

  open_file_cache max=200000 inactive=20s;
  open_file_cache_valid 30s;
  open_file_cache_min_uses 2;
  open_file_cache_errors on;

  sendfile {{ salt['pillar.get']('nginx:sendfile', 'on') }};

  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout   30;
  keepalive_requests 100000;
  reset_timedout_connection on;
  client_body_timeout 10;
  send_timeout 2;

  # nginx does not support overriding Server header by default
  # The below actually append it into nginx, JServerXXXX
  server_tokens off;
  add_header Server {{ salt['pillar.get']('lighttpd:server_tag') }} always;

  gzip on;

  #include /etc/nginx/conf.d/*.conf;

  {% for vhosts_conf in salt['pillar.get']('vhost', {}) %}
  include "/etc/nginx/conf.d/{{ vhosts_conf }}.conf";
  {% endfor %}

  {% set app = salt['environ.get']('APP_NAMESPACE') %}
  {% if app == 'papi-container' %}
  include /etc/nginx/conf.d/papi.conf;
  {% endif %}

  server {
    listen 80 default_server;

    location / {
      root /var/www/html;
      index index.php index.html index.htm;

      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include /etc/nginx/fastcgi_params;
    }
  }

  server {
    listen 8008;

    root {{ salt['pillar.get']('app:papi:api_support_path', '/var/www/support') }};

    location / {
      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include /etc/nginx/fastcgi_params;
    }

    location /server-status {
      stub_status on;
    }
  }

}
